package com.xf.h.qiaoqian.web;

import cn.hutool.core.convert.NumberChineseFormatter;
import cn.hutool.core.lang.UUID;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xf.h.qiaoqian.entity.TbQiaoqian;
import com.xf.h.qiaoqian.service.TbQiaoqianService;
import com.xf.h.qiaoqian.vo.LastAll;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@Controller
@AllArgsConstructor
public class IndexController {
    private final TbQiaoqianService tbQiaoqianService;

    @GetMapping("/")
    public String index(Model model) {
        // 进入首页
        return "index";
    }

    @GetMapping("/hz")
    public String hz(Model model) {
        // 进汇总页面
        return "hz";
    }

    @GetMapping("get")
    @ResponseBody
    public TbQiaoqian get(String id){
        return tbQiaoqianService.getById(id);
    }

    @GetMapping("bestNew")
    @ResponseBody
    public TbQiaoqian bestNew(String id){
        return tbQiaoqianService.bestNew();
    }


    @PostMapping("list")
    @ResponseBody
    public List<TbQiaoqian> list(String name){
        LambdaQueryWrapper<TbQiaoqian> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TbQiaoqian::getDelFlag,"0");
        if (StrUtil.isNotBlank(name)) {
            wrapper.like(TbQiaoqian::getName,name);
        }
        wrapper.orderByDesc(TbQiaoqian::getIndexSort);
        return tbQiaoqianService.list(wrapper);
    }

    /**
     * 金额转中文大写
     * @param moneyNum 金额
     */
    @GetMapping("toMoneyCn")
    @ResponseBody
    public Map<String,String> toMoneyCn(Double moneyNum){
        String moneyCn = NumberChineseFormatter.format(moneyNum, true, true);
        Map<String,String> map = new HashMap<>();
        map.put("moneyCn",moneyCn);
        return map;
    }

    @PostMapping("saveOrUpdate")
    @ResponseBody
    public Map<String,String> saveOrUpdate(@RequestBody TbQiaoqian qiaoqian){
        String id = qiaoqian.getId();
        TbQiaoqian db = null;
        if (StrUtil.isNotBlank(id)) {
            db = tbQiaoqianService.getById(id);
        }

        // 拿取最大序号
        int maxIndexSort = tbQiaoqianService.getMaxIndexSort() + 1;

        if (db == null) {
            // 新增
            qiaoqian.setId(UUID.fastUUID().toString(true));
            qiaoqian.setCalc("1");

            if (maxIndexSort == 36) {
                // 跳过第36个
                TbQiaoqian qiaoqian36 = new TbQiaoqian();
                qiaoqian36.setId(UUID.fastUUID().toString(true));
                // 不统计
                qiaoqian36.setIndexSort(maxIndexSort);
                qiaoqian36.setCalc("0");
                qiaoqian36.setCreateTime(new Date());
                qiaoqian36.setDelFlag("0");
                qiaoqian36.setComeOrTo("收取");
                tbQiaoqianService.save(qiaoqian36);
                maxIndexSort ++;
            }
            qiaoqian.setIndexSort(maxIndexSort);
        }

        qiaoqian.setCreateTime(new Date());
        qiaoqian.setDelFlag("0");

        if (StrUtil.equals(qiaoqian.getComeOrTo(),"支出")) {
            qiaoqian.setIndexSort(0);
        }


        tbQiaoqianService.saveOrUpdate(qiaoqian);
        Map<String,String> map = new HashMap<>();
        map.put("code","1");
        return map;
    }

    @PostMapping("delete")
    @ResponseBody
    public Map<String,String> delete(String id){
        tbQiaoqianService.removeById(id);
        Map<String,String> map = new HashMap<>();
        map.put("code","1");
        return map;
    }

    @PostMapping("pageQueryHz")
    @ResponseBody
    public Map<String,Object> pageQueryHz(int pageSize,int pageIndex){
        LambdaQueryWrapper<TbQiaoqian> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TbQiaoqian::getDelFlag,"0");
        wrapper.eq(TbQiaoqian::getComeOrTo,"收取");
        wrapper.orderByAsc(TbQiaoqian::getIndexSort);

        Page<TbQiaoqian> page = new Page<>((pageIndex+1),pageSize);


        Page<TbQiaoqian> returnPage = tbQiaoqianService.page(page, wrapper);

        Map<String,Object> pageMap = new LinkedHashMap<>();
        pageMap.put("data",returnPage.getRecords());
        pageMap.put("total",page.getTotal());

        return pageMap;
    }

    @GetMapping("lastAll")
    @ResponseBody
    public LastAll lastAll(String id){
        LastAll all = new LastAll();
        // 总体收入
        LambdaQueryWrapper<TbQiaoqian> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TbQiaoqian::getDelFlag,"0");
        wrapper.eq(TbQiaoqian::getComeOrTo,"收取");
        wrapper.eq(TbQiaoqian::getCalc,"1");
        List<TbQiaoqian> list = tbQiaoqianService.list(wrapper);
        if (!list.isEmpty()) {
            list.forEach(t->{
                Double in = all.getIn() == null ? 0 :all.getIn();
                Double moneyNum = t.getMoneyNum() == null ? 0D : t.getMoneyNum();
                all.setIn(in + moneyNum);
            });
        }else {
            all.setIn(0D);
        }


        // 总体支出
        wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TbQiaoqian::getDelFlag,"0");
        wrapper.eq(TbQiaoqian::getComeOrTo,"支出");
        wrapper.eq(TbQiaoqian::getCalc,"1");
        list = tbQiaoqianService.list(wrapper);
        if (!list.isEmpty()) {
            list.forEach(t->{
                Double to = all.getTo() == null ? 0D : all.getTo();
                Double moneyNum = t.getMoneyNum() == null ? 0D : t.getMoneyNum();
                all.setTo(to + moneyNum);
            });
        }else {
            all.setTo(0D);
        }
        //汇总
        all.setAll(all.getIn() - all.getTo());

        //汇总大写
        String moneyCn = NumberChineseFormatter.format(all.getAll(), true, true);

        all.setAllCn(moneyCn);


        return all;
    }


}